{{- define "ebpf_exporter_resources" }}
cpu: 10m
memory: 100Mi
{{- end }}

{{- if .Values.monitoringKubernetes.ebpfExporterEnabled }}
  {{- if (.Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
---
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: ebpf-exporter
  namespace: d8-monitoring
  {{- include "helm_lib_module_labels" (list . (dict "app" "ebpf-exporter" "workload-resource-policy.deckhouse.io" "every-node")) | nindent 2 }}
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: DaemonSet
    name: ebpf-exporter
  updatePolicy:
    updateMode: "Auto"
  resourcePolicy:
    containerPolicies:
    - containerName: ebpf-exporter
      minAllowed:
        {{- include "ebpf_exporter_resources" . | nindent 8 }}
      maxAllowed:
        cpu: 20m
        memory: 150Mi
    {{- include "helm_lib_vpa_kube_rbac_proxy_resources" . | nindent 4 }}
  {{- end }}
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: ebpf-exporter
  namespace: d8-monitoring
  {{- include "helm_lib_module_labels" (list . (dict "app" "ebpf-exporter")) | nindent 2 }}
spec:
  updateStrategy:
    rollingUpdate:
      maxUnavailable: 1
    type: RollingUpdate
  selector:
    matchLabels:
      app: ebpf-exporter
  template:
    metadata:
      labels:
        app: ebpf-exporter
      name: ebpf-exporter
    spec:
      hostNetwork: true
      dnsPolicy: ClusterFirstWithHostNet
      hostPID: true
      serviceAccountName: ebpf-exporter
      {{- include "helm_lib_priority_class" (tuple . "system-node-critical") | nindent 6 }}
      {{- include "helm_lib_tolerations" (tuple . "any-node") | nindent 6 }}
      {{- include "helm_lib_module_pod_security_context_run_as_user_root" . | nindent 6 }}
      containers:
      - image: {{ include "helm_lib_module_image" (list . "ebpfExporter") }}
        name: ebpf-exporter
        {{- include "helm_lib_module_container_security_context_privileged" . | nindent 8 }}
        env:
        - name: EBPF_EXPORTER_BIN_PATH
          value: "/usr/local/bin/ebpf_exporter"
        - name: EBPF_EXPORTER_CONFIG_DIR
          value: "/metrics"
        - name: EBPF_EXPORTER_CONFIG_NAMES
          value: "oomkill"
        - name: EBPF_EXPORTER_LISTEN_ADDRESS
          value: "127.0.0.1:9435"
        volumeMounts:
        - name: host-sys
          mountPath: /sys
          readOnly: true
        - name: host-usr-share-misc
          mountPath: /usr/share/misc
          readOnly: true
        resources:
          requests:
            {{- include "helm_lib_module_ephemeral_storage_logs_with_extra" 10 | nindent 12 }}
{{- if not ( .Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
            {{- include "ebpf_exporter_resources" . | nindent 12 }}
{{- end }}
      - name: kube-rbac-proxy
        {{- include "helm_lib_module_container_security_context_read_only_root_filesystem" . | nindent 8 }}
        image: {{ include "helm_lib_module_common_image" (list . "kubeRbacProxy") }}
        args:
        - "--secure-listen-address=$(KUBE_RBAC_PROXY_LISTEN_ADDRESS):9434"
        - "--v=2"
        - "--logtostderr=true"
        - "--stale-cache-interval=1h30m"
        env:
        - name: KUBE_RBAC_PROXY_LISTEN_ADDRESS
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: KUBE_RBAC_PROXY_CONFIG
          value: |
            upstreams:
            - upstream: http://127.0.0.1:9435/metrics
              path: /metrics
              authorization:
                resourceAttributes:
                  namespace: d8-monitoring
                  apiGroup: apps
                  apiVersion: v1
                  resource: daemonsets
                  subresource: prometheus-metrics
                  name: ebpf-exporter
        ports:
        - containerPort: 9434
          name: https-metrics
        resources:
          requests:
            {{- include "helm_lib_module_ephemeral_storage_only_logs" . | nindent 12 }}
{{- if not ( .Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
            {{- include "helm_lib_container_kube_rbac_proxy_resources" . | nindent 12 }}
{{- end }}
      volumes:
      - name: host-sys
        hostPath:
          path: /sys
          type: Directory
      - name: host-usr-share-misc
        hostPath:
          path: /usr/share/misc/
          type: Directory
      imagePullSecrets:
      - name: deckhouse-registry
{{- end }}

